package net.tavion.angelboot.common.utils;

import java.time.LocalDateTime;
import java.util.Random;

/**
 * @author : Rui
 * @date : 2019/4/19 10:58
 **/
public class GenerateIdUtils {

    /**
     * generator requestId
     *
     * @return
     */
    public static String generateRequestId() {
        int seed = (int) (Math.random() * 900 + 100);
        return DateTimeUtils.formatLocalDateMillsTime(LocalDateTime.now()) + seed;
    }

    /**
     * generator userId
     *
     * @return
     */
    public static Integer generateUserId() {
        int seed = (int) (Math.random() * 900 + 100);
        return Integer.parseInt(DateTimeUtils.formatLocalDateMillsTime(LocalDateTime.now()) + seed);
    }

    /**
     * generator userId
     *
     * @return
     */
    public static Integer generateSeed() {
        return (int) (Math.random() * 90000 + 10000);
    }


    /**
     * generator nickName
     *
     * @return
     */
    public static String generateNickName() {
        return GenerateIdUtils.generateString(10);
    }

    /**
     * generator once token
     *
     * @return
     */
    public static String generateOnceToken() {
        return GenerateIdUtils.generateString(8);
    }

    /**
     * generate String contain char and number
     *
     * @param length the string length
     * @return generate string
     */
    private static String generateString(int length) {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String str = random.nextInt(2) % 2 == 0 ? "num" : "char";
            // generate char and num
            if ("char".equalsIgnoreCase(str)) {
                int nextInt = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val.append((char) (nextInt + random.nextInt(26)));
            } else {
                val.append(String.valueOf(random.nextInt(10)));
            }
        }
        return val.toString();
    }

    /**
     * generate password
     *
     * @return
     */
    public static String generatePassword() {
        StringBuilder val = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            val.append((char) (random.nextInt(26) + 97));
        }
        char[] charArr = {'!', '@', '#', '$'};
        val.append(charArr[random.nextInt(4)]);
        for (int i = 0; i < 4; i++) {
            val.append(random.nextInt(10));
        }
        return val.toString();
    }
}
